package com.audiocn.dc;

import java.util.ArrayList;
import java.util.List;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.audiocn.manager.BaseManager;
import com.audiocn.manager.PostersManager;
import com.audiocn.model.PostersChanelModel;
import com.audiocn.poster.Application;
import com.audiocn.poster.Configs;
import com.audiocn.poster.R;
/**
 * 从街旁获取的当前地点的地址信息列表DC
 */
public class PostersChanelsListDC extends BaseDC implements OnItemClickListener {

	Button back;
	public ListView listView;
	ProgressDialog progress;
	public MyAdapter adapter;
	TextView textView;
	
	public List<PostersChanelModel> data = null;
	
	public void recycle() {
		dismissDialog();
		if (data != null) {
			data.clear();
		}
		data = null;
	}
	
	public PostersChanelsListDC(Context context, int layoutId, final BaseManager manager) {
		super(context, layoutId, manager);
		
		back = (Button) this.findViewById(R.id.pc_back);
		back.setOnClickListener(this);
		listView = (ListView) this.findViewById(R.id.pc_list);
		data = new ArrayList<PostersChanelModel>();
		adapter = new MyAdapter(context);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(this);
		progress = new ProgressDialog(context);
		
		progress.setOnKeyListener(new DialogInterface.OnKeyListener() {
			@Override
			public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_BACKSLASH) {
					manager.sendEmptyMessage(Configs.STOP_ALL);
				}
				return false;
			}
		});
	}
	
	public void showDialog() {
		progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		progress.setIndeterminate(false);
		progress.setMessage("正在请求数据.....");
		progress.setMax(1000);
		progress.setProgress(1);
		progress.show();
	}
	public void showDialog(String msg) {
		progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		progress.setIndeterminate(false);
		progress.setMessage(msg);
		progress.setMax(1000);
		progress.setProgress(1);
		progress.show();
	}
	public void showTipDialog(String msg) {
		Toast.makeText(context, msg, 0).show();
	}
	
	public void dismissDialog() {
		if(progress.isShowing() == true) {
			progress.dismiss();
		}
	}
	
	public ListView getListView() {
		return listView;
	}

	public void setListView(ListView listView) {
		this.listView = listView;
	}

	public List<PostersChanelModel> getData() {
		return data;
	}

	public void setData(List<PostersChanelModel> data) {
		this.data = data;
	}
	public MyAdapter getAdapter() {
		return adapter;
	}

	public void setAdapter(MyAdapter adapter) {
		this.adapter = adapter;
	}

	@Override
	public void onClicked(View v) {
		switch (v.getId()) {
		case R.id.pc_back:
			manager.sendMessage(manager.obtainMessage(110, 3, 0));
			break;
		default:
			manager.sendEmptyMessage(v.getId());
			break;
		}
		super.onClicked(v);
	}
	
	public class MyAdapter extends BaseAdapter {

		private LayoutInflater inflater = null;
		
		public MyAdapter(Context context) {
			inflater = LayoutInflater.from(context);
		}
		
		@Override
		public int getCount() {
			return data.size();
		}

		@Override
		public Object getItem(int position) {
			return data.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
//			   TextView mTextView = new TextView(getContext());
//
//	       mTextView.setTextColor(Color.WHITE);//设置字体颜色
//	       mTextView.setTextSize(20);//设置字体大小
//	 
//	       mTextView.setText(strArr[position]);//需要重新添加字体
//	            return mTextView;
//	        }
			
			ViewHolder viewHolder;
			View v;
			if (convertView == null) {
				viewHolder = new ViewHolder();
				v = inflater.inflate(Application.getLayoutId(R.layout.posters_chanels_list_item), null);
				viewHolder.title = (TextView) v.findViewById(R.id.pcli_title);
				viewHolder.desc = (TextView) v.findViewById(R.id.pcli_desc);
				v.setTag(viewHolder);
			} else {
				v = convertView;
				viewHolder = (ViewHolder) v.getTag();
			}
			String name = data.get(position).getName();
			String desc = data.get(position).getDesc();
			viewHolder.title.setText(name);
			if (desc == null || desc.trim().equals("")) {
				desc = name;
			} else {
				desc = data.get(position).getDesc();
			}
			viewHolder.desc.setText(desc);
			return v;
		}
		
	}
	
	public static final class ViewHolder {
		public TextView title;
		public TextView desc;
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		SharedPreferences sp = context.getSharedPreferences(PostersManager.USER_POSTERS_CHANEL, Context.MODE_APPEND);
		Editor e = sp.edit();
		e.putString("chanel_id", data.get(position).getId());
		e.putString("chanel_name", data.get(position).getName());
		e.putString("chanel_desc", data.get(position).getDesc());
		e.commit();
		manager.sendMessage(manager.obtainMessage(11, position, 0));
	}

}
